# -*- coding: utf-8 -*-
"""
API响应工具类
@api-version: 1.0.0
// REF: API-NETDISK-014
"""

from flask import jsonify
from typing import Any, Dict, Optional

class ApiResponse:
    """统一API响应格式
    
    提供标准化的API响应结构，确保前后端接口一致性
    """
    
    def __init__(self, success: bool = True, code: int = 200, 
                 message: str = "操作成功", data: Any = None):
        """初始化API响应
        
        Args:
            success: 操作是否成功
            code: HTTP状态码
            message: 响应消息
            data: 响应数据
        """
        self.success = success
        self.code = code
        self.message = message
        self.data = data
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        response = {
            'success': self.success,
            'code': self.code,
            'message': self.message
        }
        
        if self.data is not None:
            response['data'] = self.data
        
        return response
    
    def to_response(self):
        """转换为Flask响应对象"""
        response = jsonify(self.to_dict())
        response.status_code = self.code
        return response
    
    @classmethod
    def success(cls, data: Any = None, message: str = "操作成功", code: int = 200):
        """创建成功响应
        
        Args:
            data: 响应数据
            message: 成功消息
            code: HTTP状态码
        
        Returns:
            ApiResponse: 成功响应实例
        """
        return cls(success=True, code=code, message=message, data=data)
    
    @classmethod
    def error(cls, message: str = "操作失败", code: int = 400, data: Any = None):
        """创建错误响应
        
        Args:
            message: 错误消息
            code: HTTP状态码
            data: 错误详情数据
        
        Returns:
            ApiResponse: 错误响应实例
        """
        return cls(success=False, code=code, message=message, data=data)
    
    @classmethod
    def not_found(cls, message: str = "资源不存在"):
        """创建404响应"""
        return cls.error(message=message, code=404)
    
    @classmethod
    def unauthorized(cls, message: str = "未授权访问"):
        """创建401响应"""
        return cls.error(message=message, code=401)
    
    @classmethod
    def forbidden(cls, message: str = "禁止访问"):
        """创建403响应"""
        return cls.error(message=message, code=403)
    
    @classmethod
    def validation_error(cls, message: str = "参数验证失败", errors: Dict = None):
        """创建参数验证错误响应"""
        data = {'errors': errors} if errors else None
        return cls.error(message=message, code=422, data=data)
    
    @classmethod
    def server_error(cls, message: str = "服务器内部错误"):
        """创建500响应"""
        return cls.error(message=message, code=500)
    
    @classmethod
    def quota_exceeded(cls, message: str = "配额已满"):
        """创建配额超限响应"""
        return cls.error(message=message, code=413)
    
    @classmethod
    def conflict(cls, message: str = "资源冲突"):
        """创建409响应"""
        return cls.error(message=message, code=409)
    
    @classmethod
    def too_many_requests(cls, message: str = "请求过于频繁"):
        """创建429响应"""
        return cls.error(message=message, code=429)
    
    def __str__(self):
        return f"ApiResponse(success={self.success}, code={self.code}, message='{self.message}')"
    
    def __repr__(self):
        return f"<ApiResponse success={self.success} code={self.code}>"